﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.JOB.users.Helper;
using DTcms.Common;

namespace DTcms.JOB.member_order.DAL
{
	/// <summary>
	/// 数据访问类:ch_company
	/// </summary>
	public partial class company
	{
		public company()
		{ }
		#region 基本方法============================
		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_company");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool UserExists(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_company");
            strSql.Append(" where user_id=@user_id");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)};
            parameters[0].Value = user_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 检查手机号码是否存在
        /// </summary>
        public bool ExistsMobile(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_company");
            strSql.Append(" where mobile=@mobile ");
            SqlParameter[] parameters = {
					new SqlParameter("@mobile", SqlDbType.NVarChar,20)};
            parameters[0].Value = mobile;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 检查企业名称是否存在
        /// </summary>
        public bool ExistsTitle(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_company");
            strSql.Append(" where title=@title ");
            SqlParameter[] parameters = {
                    new SqlParameter("@title", SqlDbType.NVarChar,20)};
            parameters[0].Value = title;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.company model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ch_company(");
            strSql.Append("user_id,title,logo,pro_remark,add_time,lianxiren,telphone,mobile,fax,address,rz_status,status,call_index,about,zuobiao,province_id,city_id,district_id,quanzi_id,category_id,rz_category_id,rz_imgurl,topimg,group_id,businessScope,sort_id,jixie_category_id,is_jixie,website,video_img,video_url)");
            strSql.Append(" values (");
            strSql.Append("@user_id,@title,@logo,@pro_remark,@add_time,@lianxiren,@telphone,@mobile,@fax,@address,@rz_status,@status,@call_index,@about,@zuobiao,@province_id,@city_id,@district_id,@quanzi_id,@category_id,@rz_category_id,@rz_imgurl,@topimg,@group_id,@businessScope,@sort_id,@jixie_category_id,@is_jixie,@website,@video_img,@video_url)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@title", SqlDbType.NVarChar,100),
					new SqlParameter("@logo", SqlDbType.NVarChar,100),
					new SqlParameter("@pro_remark", SqlDbType.NVarChar,255),
					new SqlParameter("@add_time", SqlDbType.DateTime),
					new SqlParameter("@lianxiren", SqlDbType.NVarChar,20),
					new SqlParameter("@telphone", SqlDbType.NVarChar,20),
					new SqlParameter("@mobile", SqlDbType.NVarChar,20),
					new SqlParameter("@fax", SqlDbType.NVarChar,20),
					new SqlParameter("@address", SqlDbType.NVarChar,100),
					new SqlParameter("@rz_status", SqlDbType.TinyInt,1),
					new SqlParameter("@status", SqlDbType.TinyInt,1),
                    new SqlParameter("@call_index", SqlDbType.NVarChar,20),
                    new SqlParameter("@about", SqlDbType.NText),
                    new SqlParameter("@zuobiao", SqlDbType.NVarChar,100),
                    new SqlParameter("@province_id", SqlDbType.Int,4),
                    new SqlParameter("@city_id", SqlDbType.Int,4),
                    new SqlParameter("@district_id", SqlDbType.Int,4),
                    new SqlParameter("@quanzi_id", SqlDbType.Int,4),
                    new SqlParameter("@category_id", SqlDbType.Int,4),
                    new SqlParameter("@rz_category_id", SqlDbType.Int,4),
                    new SqlParameter("@rz_imgurl", SqlDbType.NVarChar,255),
                    new SqlParameter("@topimg", SqlDbType.NVarChar,255),
                    new SqlParameter("@group_id", SqlDbType.Int,4),
                    new SqlParameter("@businessScope", SqlDbType.NVarChar,255),
                    new SqlParameter("@sort_id", SqlDbType.Int, 4),
                    new SqlParameter("@jixie_category_id", SqlDbType.Int, 4),
                    new SqlParameter("@is_jixie", SqlDbType.Int, 4),
                    new SqlParameter("@website", SqlDbType.NVarChar,255),
                    new SqlParameter("@video_img", SqlDbType.NVarChar,255),
                    new SqlParameter("@video_url", SqlDbType.NVarChar,255)
            };

        parameters[0].Value = model.user_id;            			
			parameters[1].Value = model.title;            			
			parameters[2].Value = model.logo;            			
			parameters[3].Value = model.pro_remark;            			
			parameters[4].Value = model.add_time;            			
			parameters[5].Value = model.lianxiren;            			
			parameters[6].Value = model.telphone;            			
			parameters[7].Value = model.mobile;            			
			parameters[8].Value = model.fax;            			
			parameters[9].Value = model.address;            			
			parameters[10].Value = model.rz_status;            			
			parameters[11].Value = model.status;
            parameters[12].Value = model.call_index;
            parameters[13].Value = model.about;
            parameters[14].Value = model.zuobiao;
            parameters[15].Value = model.province_id;
            parameters[16].Value = model.city_id;
            parameters[17].Value = model.district_id;
            parameters[18].Value = model.quanzi_id;
            parameters[19].Value = model.category_id;
            parameters[20].Value = model.rz_category_id;
            parameters[21].Value = model.rz_imgurl;
            parameters[22].Value = model.topimg;
            parameters[23].Value = model.group_id;
            parameters[24].Value = model.businessScope;
            parameters[25].Value = model.Sort_id;
            parameters[26].Value = model.Jixie_category_id;
            parameters[27].Value = model.Is_jixie;
            parameters[28].Value = model.website;
            parameters[29].Value = model.video_img;
            parameters[30].Value = model.video_url;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.company model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ch_company set ");
			                                                
            strSql.Append("user_id = @user_id,");                                    
            strSql.Append("title = @title,");                                    
            strSql.Append("logo = @logo,");                                    
            strSql.Append("pro_remark = @pro_remark,");                                    
            strSql.Append("add_time = @add_time,");                                    
            strSql.Append("lianxiren = @lianxiren,");                                    
            strSql.Append("telphone = @telphone,");                                    
            strSql.Append("mobile = @mobile,");                                    
            strSql.Append("fax = @fax,");                                    
            strSql.Append("address = @address,");                                    
            strSql.Append("rz_status = @rz_status,");                                    
            strSql.Append("status = @status,");
            strSql.Append("call_index = @call_index,");
            strSql.Append("about = @about,");
            strSql.Append("zuobiao = @zuobiao,");
            strSql.Append("province_id = @province_id,");
            strSql.Append("city_id = @city_id,");
            strSql.Append("district_id = @district_id,");
            strSql.Append("quanzi_id = @quanzi_id,");
            strSql.Append("category_id = @category_id,");
            strSql.Append("rz_category_id = @rz_category_id,");
            strSql.Append("rz_imgurl = @rz_imgurl,");
            strSql.Append("topimg = @topimg,");
            strSql.Append("group_id = @group_id,");
            strSql.Append("businessScope = @businessScope,");
            strSql.Append("sort_id = @sort_id,");
            strSql.Append("jixie_category_id = @jixie_category_id,");
            strSql.Append("is_jixie = @is_jixie,");
            strSql.Append("website = @website,");
            strSql.Append("video_img = @video_img,");
            strSql.Append("video_url = @video_url");
            strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
			            new SqlParameter("@id", SqlDbType.Int,4) ,            
                        new SqlParameter("@user_id", SqlDbType.Int,4),
                        new SqlParameter("@title", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@logo", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@pro_remark", SqlDbType.NVarChar,255),           
                        new SqlParameter("@add_time", SqlDbType.DateTime) ,            
                        new SqlParameter("@lianxiren", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@telphone", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@mobile", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@fax", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@address", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@rz_status", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@status", SqlDbType.TinyInt,1),
                        new SqlParameter("@call_index", SqlDbType.NVarChar,20),
                        new SqlParameter("@about", SqlDbType.NText),
                        new SqlParameter("@zuobiao", SqlDbType.NVarChar,100),
                        new SqlParameter("@province_id", SqlDbType.Int,4),
                        new SqlParameter("@city_id", SqlDbType.Int,4),
                        new SqlParameter("@district_id", SqlDbType.Int,4),
                        new SqlParameter("@quanzi_id", SqlDbType.Int,4),
                        new SqlParameter("@category_id", SqlDbType.Int,4),
                        new SqlParameter("@rz_category_id", SqlDbType.Int,4),
                        new SqlParameter("@rz_imgurl", SqlDbType.NVarChar,255),
                        new SqlParameter("@topimg", SqlDbType.NVarChar,255),
                        new SqlParameter("@group_id", SqlDbType.Int,4),
                        new SqlParameter("@businessScope", SqlDbType.NVarChar,255),
                        new SqlParameter("@sort_id", SqlDbType.NVarChar, 255),
            new SqlParameter("@jixie_category_id", SqlDbType.Int, 4),
            new SqlParameter("@is_jixie", SqlDbType.Int, 4),
            new SqlParameter("@website", SqlDbType.NVarChar, 255),
            new SqlParameter("@video_img", SqlDbType.NVarChar, 255),
            new SqlParameter("@video_url", SqlDbType.NVarChar, 255)
            };


        parameters[0].Value = model.id;                        
            parameters[1].Value = model.user_id;                        
            parameters[2].Value = model.title;                        
            parameters[3].Value = model.logo;                        
            parameters[4].Value = model.pro_remark;                        
            parameters[5].Value = model.add_time;                        
            parameters[6].Value = model.lianxiren;                        
            parameters[7].Value = model.telphone;                        
            parameters[8].Value = model.mobile;                        
            parameters[9].Value = model.fax;                        
            parameters[10].Value = model.address;                        
            parameters[11].Value = model.rz_status;                        
            parameters[12].Value = model.status;
            parameters[13].Value = model.call_index;
            parameters[14].Value = model.about;
            parameters[15].Value = model.zuobiao;
            parameters[16].Value = model.province_id;
            parameters[17].Value = model.city_id;
            parameters[18].Value = model.district_id;
            parameters[19].Value = model.quanzi_id;
            parameters[20].Value = model.category_id;
            parameters[21].Value = model.rz_category_id;
            parameters[22].Value = model.rz_imgurl;
            parameters[23].Value = model.topimg;
            parameters[24].Value = model.group_id;
            parameters[25].Value = model.businessScope;
            parameters[26].Value = model.Sort_id;
            parameters[27].Value = model.Jixie_category_id;
            parameters[28].Value = model.Is_jixie;
            parameters[29].Value = model.website;
            parameters[30].Value = model.video_img;
            parameters[31].Value = model.video_url;

            int rows =DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_company ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.company GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,user_id,title,logo,pro_remark,add_time,lianxiren,telphone,mobile,fax,address,rz_status,status,call_index,about,zuobiao,province_id,city_id,district_id,quanzi_id,category_id,rz_category_id,rz_imgurl,topimg,group_id,businessScope,sort_id,jixie_category_id,is_jixie,website,video_img,video_url");
            strSql.Append(" from ch_company");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.company model = new Model.company();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,user_id,title,logo,pro_remark,add_time,lianxiren,telphone,mobile,fax,address,rz_status,status,call_index,about,zuobiao,province_id,city_id,district_id,quanzi_id,category_id,rz_category_id,rz_imgurl,topimg,group_id,businessScope,sort_id,jixie_category_id,is_jixie,website,video_img,video_url");
            strSql.Append(" FROM ch_company ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ch_company");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion
        
        #region 扩展方法============================
        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetTitle(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 title from ch_company");
            strSql.Append(" where id=" + id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "-";
            }
            return title;
        }

        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetUserTitle(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 title from ch_company");
            strSql.Append(" where user_id=" + user_id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "-";
            }
            return title;
        }

        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetMobile(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 mobile from ch_company");
            strSql.Append(" where id=" + id);
            string mobile = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(mobile))
            {
                return "-";
            }
            return mobile;
        }

        /// <summary>
        /// 返回站点对应的导航ID
        /// </summary>
        public int GetCompanyID(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id from ch_company");
            strSql.Append(" where user_id=" + user_id);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;
        }
        public int GetIDByTitle(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id from ch_company");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters = {
                    new SqlParameter("@title", SqlDbType.NVarChar,50)};
            parameters[0].Value = title;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;
        }

        /// <summary>
        /// 返回站点对应的导航ID
        /// </summary>
        public int GetCoID(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id from ch_company");
            strSql.Append(" where mobile='" + mobile+"'");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_company set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateFields(int user_id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_company set " + strValue);
            strSql.Append(" where user_id=" + user_id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.company DataRowToModel(DataRow row)
        {
            Model.company model = new Model.company();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["user_id"] != null && row["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(row["user_id"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["logo"] != null)
                {
                    model.logo = row["logo"].ToString();
                }
                if (row["pro_remark"] != null)
                {
                    model.pro_remark = row["pro_remark"].ToString();
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["lianxiren"] != null)
                {
                    model.lianxiren = row["lianxiren"].ToString();
                }
                if (row["telphone"] != null)
                {
                    model.telphone = row["telphone"].ToString();
                }
                if (row["mobile"] != null)
                {
                    model.mobile = row["mobile"].ToString();
                }
                if (row["fax"] != null)
                {
                    model.fax = row["fax"].ToString();
                }
                if (row["address"] != null)
                {
                    model.address = row["address"].ToString();
                }
                if (row["rz_status"] != null && row["rz_status"].ToString() != "")
                {
                    model.rz_status = int.Parse(row["rz_status"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["call_index"] != null)
                {
                    model.call_index = row["call_index"].ToString();
                }
                if (row["about"] != null)
                {
                    model.about = row["about"].ToString();
                }
                if (row["zuobiao"] != null)
                {
                    model.zuobiao = row["zuobiao"].ToString();
                }
                if (row["province_id"] != null && row["province_id"].ToString() != "")
                {
                    model.province_id = int.Parse(row["province_id"].ToString());
                }
                if (row["city_id"] != null && row["city_id"].ToString() != "")
                {
                    model.city_id = int.Parse(row["city_id"].ToString());
                }
                if (row["district_id"] != null && row["district_id"].ToString() != "")
                {
                    model.district_id = int.Parse(row["district_id"].ToString());
                }
                if (row["quanzi_id"] != null && row["quanzi_id"].ToString() != "")
                {
                    model.quanzi_id = int.Parse(row["quanzi_id"].ToString());
                }
                if (row["category_id"] != null && row["category_id"].ToString() != "")
                {
                    model.category_id = int.Parse(row["category_id"].ToString());
                }
                if (row["rz_category_id"] != null && row["rz_category_id"].ToString() != "")
                {
                    model.rz_category_id = int.Parse(row["rz_category_id"].ToString());
                }
                if (row["rz_imgurl"] != null)
                {
                    model.rz_imgurl = row["rz_imgurl"].ToString();
                }
                if (row["topimg"] != null)
                {
                    model.topimg = row["topimg"].ToString();
                }
                if (row["group_id"] != null && row["group_id"].ToString() != "")
                {
                    model.group_id = int.Parse(row["group_id"].ToString());
                }
                
                if (row["businessScope"] != null && row["businessScope"].ToString() != "")
                {
                    model.businessScope = row["businessScope"].ToString();
                }
                if (row["sort_id"] != null && row["sort_id"].ToString() != "")
                {
                    model.Sort_id = int.Parse(row["sort_id"].ToString());
                }
                if (row["jixie_category_id"] != null && row["jixie_category_id"].ToString() != "")
                {
                    model.Jixie_category_id = int.Parse(row["jixie_category_id"].ToString());
                }
                if (row["is_jixie"] != null && row["is_jixie"].ToString() != "")
                {
                    model.Is_jixie = int.Parse(row["is_jixie"].ToString());
                }
                if (row["website"] != null && row["website"].ToString() != "")
                {
                    model.website =row["website"].ToString();
                }
                if (row["video_img"] != null && row["video_img"].ToString() != "")
                {
                    model.video_img = row["video_img"].ToString();
                }
                if (row["video_url"] != null && row["video_url"].ToString() != "")
                {
                    model.video_url = row["video_url"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 返回公司LOGO
        /// </summary>
        public string GetLogo(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 logo from ch_company");
            strSql.Append(" where id=" + id);
            string mobile = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(mobile))
            {
                return "";
            }
            return mobile;
        }
        #endregion
    }
}
